package com.itheima.test;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisTest1 {
     @Test
    public void testSelectAll() throws IOException {
          int i=1;
        //获取
         String resource = "mybatis-config.xml";
         InputStream inputStream = Resources.getResourceAsStream(resource);
         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

         SqlSession sqlSession = sqlSessionFactory.openSession();

         BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

          Brand byId = mapper.findById(i);
         System.out.println(byId);

         sqlSession.close();
     }
    @Test
    public void selectByCondition() throws IOException {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%" + companyName + "%";
        brandName="%" + brandName + "%";
        Brand brand=new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectByCondition(brand);
        System.out.println(brands);

        sqlSession.close();
    }
    @Test
    public void selectByConditionSingle() throws IOException {
        int status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%" + companyName + "%";
        brandName="%" + brandName + "%";
        Brand brand=new Brand();
        //brand.setStatus(status);
       // brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectByConditionSingle(brand);
        System.out.println(brands);

        sqlSession.close();
    }
    @Test
    public void add() throws IOException {
        int status=1;
        String companyName="小米6";
        String brandName="小米";
        String description="为发烧而生";
        Integer ordered =300;
        Brand brand=new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setDescription(description);
        brand.setOrdered(ordered);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.add(brand);
        Integer id = brand.getId();
        System.out.println(id);
        sqlSession.commit();

        sqlSession.close();
    }
    @Test
    public void update() throws IOException {
        int id=5;
        int status=1;
        String companyName="小米68";
        String brandName="小米";
        String description="为发烧而生,就会烧坏";
        Integer ordered =300;
        Brand brand=new Brand();
        brand.setId(id);
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setDescription(description);
        brand.setOrdered(ordered);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
       mapper.update(brand);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void delete() throws IOException {
        int id=4;
//        int status=1;
//        String companyName="小米68";
//        String brandName="小米";
//        String description="为发烧而生,就会烧坏";
//        Integer ordered =300;
//        Brand brand=new Brand();
//        brand.setId(id);
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setDescription(description);
//        brand.setOrdered(ordered);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deletebyid(id);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deletemore() throws IOException {
        int [] ids={7,8,9};
//        int status=1;
//        String companyName="小米68";
//        String brandName="小米";
//        String description="为发烧而生,就会烧坏";
//        Integer ordered =300;
//        Brand brand=new Brand();
//        brand.setId(id);
//        brand.setStatus(status);
//        brand.setBrandName(brandName);
//        brand.setCompanyName(companyName);
//        brand.setDescription(description);
//        brand.setOrdered(ordered);
        //获取
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.delete(ids);
        sqlSession.commit();
        sqlSession.close();
    }
}
